<?php
/**
 * Created by PhpStorm.
 * User: zws
 * Date: 2018/9/8
 * Time: 11:02
 * 检测人员管理
 */
namespace app\index\service;

use app\common\JwtToken;
use app\common\model\SmsCodeModel;
use app\common\model\UserModel;
use think\Exception;

class UserService
{
    protected static $userModel;
    /**
     * UserService constructor.
     * @param UserModel $userModel
     * @author zws
     */
    public function __construct(UserModel $userModel)
    {
        self::$userModel = $userModel;
    }

    /**
     * 发送验证码
     * @param $param
     * @author zws
     */
    public function sendCode($param)
    {
        if(empty($param))
        {
            app_fail(9997);//参数错误
        }
        if(!isset($param['phone']) || empty($param['phone']))
        {
            app_fail(9992);//手机号不能为空
        }
        if(strlen($param['phone']) != 11)
        {
            app_fail(9987);
        }
        $phone = $param['phone'];
        $smsCode = mt_rand(100000, 999999);
        try{
            app_sms($phone, (String) $smsCode);
            $smsCodeModel = new SmsCodeModel();
            $data['phone'] = $phone;
            $data['code'] = $smsCode;
            $smsCodeModel->save($data);
        }catch (Exception $e){
            app_fail(9991);//验证码发送失败
        }
    }

    /**
     * 用户注册
     * @param $param
     * @return bool
     * @author zws
     */
    public function register($param){
        if(empty($param) || !isset($param['phone']) || empty($param['phone']) || !isset($param['password']) || empty($param['password']))
        {
            app_fail(9997);//参数错误
        }
        if(!isset($param['verCode']) || empty($param['verCode']))
        {
            app_fail(9988);
        }

        $user = self::$userModel;
        $userInfo = $user->where("phone", "=", $param['phone'])->find();
        if($userInfo)
        {
            app_fail(9995);//用户名已存在
        }
        if(!$this->verificationCode($param['phone'], $param['verCode']))
        {
            app_fail(9990);
        }
        $user->phone = $param['phone'];
        $user->password = md5($param['password']);
        $user->user_name = isset($param['user_name']) ? $param['user_name'] : '';
        $user->work_num = isset($param['work_num']) ? $param['work_num'] : '';
        $user->company = isset($param['company']) ? $param['company'] : '';
        try{
            $user->save();
        }catch (Exception $e){
            app_fail(9899);//用户编辑失败
        }
    }

    /**
     * 用户登录
     * @param $param
     * @return array|null|\PDOStatement|string|\think\Model
     * @author zws
     */
    public function login($param)
    {
        if(empty($param) || !isset($param['phone']) || empty($param['phone']) |!isset($param['password']) || empty($param['password']))
        {
            app_fail(9997);//参数错误
        }
        $user = self::$userModel;
        $userInfo = $user->where(['phone'=>$param['phone'], 'password'=>md5($param['password'])])->find();
        if(!$userInfo)
        {
            app_fail(7997);//用户名密码错误
        }

        // 生成 TOKEN 保存用户信息
        $token = JwtToken::createToken($userInfo->id);

        $data = array(
            'user_name' => $userInfo->user_name,
            'phone'     => $userInfo->phone,
            'work_num'  => $userInfo->work_num,
            'company'   => $userInfo->company,
            'token'     => $token,
        );
        return $data;
    }

    /**
     * 找回密码
     * @param $param
     * @return static
     * @author zws
     */
    public function getPwd($param)
    {
        if(empty($param) || !isset($param['phone']) || empty($param['phone']) || !isset($param['password']) || empty
            ($param['password'])){
            app_fail(9997);//参数错误
        }
        if(!isset($param['verCode']) || empty($param['verCode']))
        {
            app_fail(9988);
        }
        if(!$this->verificationCode($param['phone'], $param['verCode']))
        {
            app_fail(9990);
        }
        $user = self::$userModel;
        $userInfo = $user->where("phone", "=", $param['phone'])->find();
        if(!$userInfo)
        {
            app_fail(7999);//用户名不已存在
        }
        try{
            $user->where('phone', '=', $param['phone'])
                ->data(['password'=>md5($param['password'])])
                ->update();
            return [];
        }catch (Exception $e){
            app_fail(9899);//用户编辑失败
        }
    }

    /**
     * 验证 短信验证码
     * @param $phone
     * @param $smsCode
     * @return bool
     * @author zws
     */
    public function verificationCode($phone, $smsCode)
    {
        $smsModel = new SmsCodeModel();
        $smsData = $smsModel->where('phone', '=', $phone)->order('id desc')
            ->find();
        $expire_time = time()-strtotime($smsData->create_time);
        if(!$smsData || $smsData['status'] == 1 || $smsData->code != $smsCode)
        {
            app_fail(9990);
        }
        if($expire_time > 600)
        {
            app_fail(9989);//验证码超时
        }
        try{
           $smsModel->save(['status'=>1], ['id'=>$smsData->id]);
        }catch (Exception $e){
            app_fail(9899);//用户编辑失败
        }
        return true;
    }
}